package com.qs.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.constant.CacheConstant;
import com.qs.constant.UserConstant;
import com.qs.exception.ServiceException;
import com.qs.model.dto.RegisterDto;
import com.qs.model.dto.UserDto;
import com.qs.model.entity.User;
import com.qs.model.vo.PhoneUserVo;
import com.qs.response.Response;
import com.qs.response.ResponseEnum;
import com.qs.service.IUserService;
import com.qs.utils.EncryptUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: LoginController
 * Package: com.qs.controller
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2024/3/31 - 11:49
 * @Version: v1.0
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/login")
@RequiredArgsConstructor
public class LoginController {

    private final RedisTemplate redisTemplate;

    private final IUserService userService;

    /**
     * 前台手机号登录接口
     * @param userVo
     * @return
     */
    @PostMapping("/front")
    public Response phoneLogin(@RequestBody PhoneUserVo userVo) {
        //判断用户是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(userVo.getPhone()),"phone",userVo.getPhone());
        User user = userService.getOne(wrapper);
        if(null == user) {
            throw new ServiceException(ResponseEnum.USER_NOT_EXITS);
        }
        //判断用户是否被禁用
        if(user.getStatus().getComment().equalsIgnoreCase(UserConstant.STATUS_FORBID)){
            throw new ServiceException(ResponseEnum.USER_FORBID);
        }
        //检验验证码是否过期
        String code = (String) redisTemplate.opsForValue().get(CacheConstant.MESSAGE_CODE + userVo.getPhone());
        if(StringUtils.isEmpty(code)){
            throw new ServiceException(ResponseEnum.CODE_EXPIRE);
        }
        //对比验证码是否正确
        if(!userVo.getCode().equalsIgnoreCase(code)){
            throw new ServiceException(ResponseEnum.CODE_ERROR);
        }
        StpUtil.login(user.getId());
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user,userDto);
        Map<String,Object> map = new HashMap<>(2);
        map.put("user",userDto);
        map.put("authorization",StpUtil.getTokenValue());
        return Response.success(map);
    }

    /**
     * 前台密码登录接口
     * @param registerDto
     * @return
     */
    @PostMapping("/front/pwd")
    public Response pwdLogin(@RequestBody RegisterDto registerDto) {
        //判断用户是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(registerDto.getNickName()),"nick_name",registerDto.getNickName());
        User user = userService.getOne(wrapper);
        if(null == user) {
            throw new ServiceException(ResponseEnum.USER_NOT_EXITS);
        }
        //判断用户是否被禁用
        if(user.getStatus().getComment().equalsIgnoreCase(UserConstant.STATUS_FORBID)){
            throw new ServiceException(ResponseEnum.USER_FORBID);
        }
        //判断密码是否正确
        if(!user.getPassword().equalsIgnoreCase(EncryptUtils.MD5Password(registerDto.getPassword()))){
            throw new ServiceException(ResponseEnum.PASSWORD_ERROR);
        }
        StpUtil.login(user.getId());
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user,userDto);
        Map<String,Object> map = new HashMap<>(2);
        map.put("user",userDto);
        map.put("authorization",StpUtil.getTokenValue());
        return Response.success(map);
    }
}
